"""The PLGen module contains the PLGen class, which is an interface between the program and the XMLHandler"""

import os
from XMLHandler import VideoList
from Export import Export

class PLGen:
    def __init__(self, videoList):
        self.videoList = videoList
        self.list = self.videoList.getList()

    def search(self):
        print(self.videoList.findByTags(self.tags))

    def getCompleteList(self):
        return self.list

    def export(self, tagstring, formatstring, filename):
        self.exportModule = Export()
        tags = []
        if tagstring != "":
            tags = tagstring
        self.exportModule.export(self.list, formatstring, filename)

    def addDirectory(self, directory, tags, rating):
        filelist = os.listdir(directory)
        for file in filelist:
            self.addItem(directory,file,tags,rating)

        #temp!
        self.saveList("test.xml")

    def addFile(self, filename, tags = "", rating = "3"):
        (folder, file) = os.path.split(filename)
        print "add" + folder + file + " " + tags + rating
        self.addItem(folder, file, tags, rating)
        print self.list

    def addItem(self, folder, file, tags = "", rating = "3"):
        item = {'folder': folder.replace("\\\\", "\\"),
                'filename': file,
                'tags': tags,
                'rating': rating,
                'action': 'new'}
        self.list.append(item)

    def getDirectory(self):
        return "D:\\Media\\Music Videos\\"

    def saveList(self, xmlfilename):
        self.videoList.updateAndSaveList(self.list, xmlfilename)
        
    def setDefaultStartupList(self, filename):
        pass
    
    def load(self, filename):
        pass
    
    def save(self, filename):
        pass